$(function () {
    $("#picture").on("change", function () {
        debugger
        var file = this.files[0];
        var rd = new FileReader();
        rd.readAsDataURL(file);//文件读取装换为base64类型
        rd.onloadend = function (e) {
            //加载完毕之后获取结果赋值给img
            document.getElementById("myAvatar").src = this.result;
        }
    })
    $.ajax({
        type: "GET",
        url: "/blog/users/getAvatar",
        data: {
            "state": 1
        },
        dataType: "JSON",
        success: function (response) {
            var text = ""
            if (response.state == "401") {
                text = "上传头像";
            } else {
                text = "修改头像";
            }
            $("#picture_upload").show();
            $("#submit").html(text);
        }
    });
    $("#submit").click(function uploadPicture() {
        var data = new FormData($("#upload")[0])
        data.append("check", 0);
        data.append("state", 1);
        console.log(data);
        $.ajax({
            type: "POST",
            url: "/blog/users/upload",
            data: data,
            "contentType": false,
            "processData": false,
            dataType: "json",
            success: function (response) {
                if (response.state == 401) {
                    check1 = confirm(response.message);
                    if (check1) {
                        var data1 = new FormData($("#upload")[0]);
                        data1.append("check", 1);
                        data1.append("state", 1);
                        $.ajax({
                            type: "POST",
                            url: "/blog/users/upload",
                            data: data1,
                            "contentType": false,
                            "processData": false,
                            dataType: "json",
                            success: function (response) {
                                alert(response.message);
                                $("#myAvatar").attr("src", "../upload/" + response.data)
                            }
                        });
                    }
                } else {
                    alert(response.message);
                    $("#myAvatar").attr("src", "../upload/" + response.data)
                }

            }
        });
    })

    $("#test").click(function () {
        var data = {
            "url": "../html/window.html",
            "width": 600,
            "height": 300
        }
        openWindow(data);
    })
    $("#next").click(function () {
        var data = {
            "url": "../html/window.html",
            "width": 600,
            "height": 300
        }
        transferToNextWindow(data);
    })


})